<!DOCTYPE html>
<html>
<head>
<title>Atomic Beam Experiment Setup</title>
<style>
  body {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    margin: 0;
  }
  canvas {
    border: 1px solid #ccc;
  }
</style>
</head>
<body>
<canvas id="physicsCanvas" width="600" height="350"></canvas>
<script>
const canvas = document.getElementById('physicsCanvas');
const ctx = canvas.getContext('2d');

// Style settings
ctx.lineWidth = 2;
ctx.strokeStyle = 'black';
ctx.fillStyle = 'black';
ctx.font = '22px "Times New Roman", serif';
ctx.textAlign = 'center';

// --- Define coordinates and dimensions ---
const ovenX = 50;
const ovenY = 100;
const ovenSize = 90;
const axisY = ovenY + ovenSize / 2;

const distanceD = 220;
const collimatorX = ovenX + ovenSize + distanceD;
const collimatorHeight = 120;
const slitGap = 10;

// --- Draw Oven (S) ---
ctx.strokeRect(ovenX, ovenY, ovenSize, ovenSize);
ctx.textBaseline = 'bottom';
ctx.fillText('S', ovenX + ovenSize / 2, ovenY - 5);
ctx.textBaseline = 'middle';
ctx.fillText('Na', ovenX + ovenSize / 2, axisY);

// --- Draw Collimator (C) ---
ctx.beginPath();
// Top part of collimator
ctx.moveTo(collimatorX, axisY - collimatorHeight / 2);
ctx.lineTo(collimatorX, axisY - slitGap / 2);
// Bottom part of collimator
ctx.moveTo(collimatorX, axisY + slitGap / 2);
ctx.lineTo(collimatorX, axisY + collimatorHeight / 2);
ctx.stroke();
ctx.textBaseline = 'bottom';
ctx.fillText('C', collimatorX, axisY - collimatorHeight / 2 - 5);


// --- Draw Central Axis (dashed line) ---
ctx.save();
ctx.setLineDash([7, 5]);
ctx.beginPath();
ctx.moveTo(ovenX + ovenSize, axisY);
ctx.lineTo(canvas.width - 20, axisY);
ctx.stroke();
ctx.restore();


// --- Draw Distance Label (d) ---
const distLineY = ovenY + ovenSize + 30;
const tickHeight = 5;
ctx.beginPath();
// Horizontal line
ctx.moveTo(ovenX + ovenSize, distLineY);
ctx.lineTo(collimatorX, distLineY);
// Left tick
ctx.moveTo(ovenX + ovenSize, distLineY - tickHeight);
ctx.lineTo(ovenX + ovenSize, distLineY + tickHeight);
// Right tick
ctx.moveTo(collimatorX, distLineY - tickHeight);
ctx.lineTo(collimatorX, distLineY + tickHeight);
ctx.stroke();
ctx.textBaseline = 'bottom';
ctx.fillText('d', ovenX + ovenSize + distanceD / 2, distLineY - 3);


// --- Draw Outgoing Beam Information ---
const arrowStartY = axisY - 25;
const arrowStartX = collimatorX + 30;
const arrowLength = 60;
const arrowEndX = arrowStartX + arrowLength;

// Arrow
ctx.beginPath();
ctx.moveTo(arrowStartX, arrowStartY);
ctx.lineTo(arrowEndX, arrowStartY);
// Arrowhead
ctx.moveTo(arrowEndX, arrowStartY);
ctx.lineTo(arrowEndX - 10, arrowStartY - 5);
ctx.moveTo(arrowEndX, arrowStartY);
ctx.lineTo(arrowEndX - 10, arrowStartY + 5);
ctx.stroke();

// "to experiment" text
ctx.textAlign = 'left';
ctx.textBaseline = 'middle';
ctx.fillText('to experiment', arrowEndX + 10, arrowStartY);

// "beam" text
ctx.textAlign = 'center';
ctx.textBaseline = 'top';
ctx.fillText('beam', collimatorX + 70, axisY + 10);


// --- Draw Figure Caption ---
ctx.textBaseline = 'bottom';
ctx.fillText('Fig. 2.42.', canvas.width / 2, canvas.height - 20);

</script>
</body>
</html>